<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="项目" prop="grantId">
        <el-select v-model="queryParams.grantId" style="width:220px" placeholder="请选择项目"  @change="grantSelectChange">
          <el-option
            v-for="dict in grantOptions"
            :key="dict.grantId"
            :label="dict.grantName"
            :value="dict.grantId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="场次名称" prop="sceneName">
        <el-input
          v-model="queryParams.sceneName"
          placeholder="请输入场次名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['grant:scene:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['grant:scene:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['grant:scene:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['grant:scene:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="sceneList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" width="55" prop="id" />
      <el-table-column label="项目" align="center" prop="grantName" />
      <el-table-column label="考点" align="center" prop="epName" />
      <el-table-column label="场次名称" align="center" prop="sceneName" />
      <el-table-column label="开始时间" align="center" prop="sceneStartTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sceneStartTime, '{y}-{m}-{d} {h}:{mi}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="sceneEndTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sceneEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报道截止时间" align="center" prop="signEndTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" width="120" prop="createUser" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['grant:scene:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['grant:scene:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-share"
            @click="getSceneQrde(scope.row)"
          >签到链接</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-share"
            @click="getRpSceneQrde(scope.row)"
          >替换链接</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改签到场次对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="项目" prop="grantId">
          <el-select v-model="form.grantId" style="width:220px" placeholder="请选择项目"  @change="grantSelectChangeByForm">
            <el-option
              v-for="dict in grantOptions"
              :key="dict.grantId"
              :label="dict.grantName"
              :value="dict.grantId"
            />
          </el-select>
         </el-form-item>
        <el-form-item label="考点" prop="exampointId">
           <el-select v-model="form.exampointId" style="width:220px"  placeholder="请选择考点"  >
            <el-option
              v-for="dict in exampointOptions"
              :key="dict.id"
              :label="dict.epName"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="场景名称" prop="sceneName">
          <el-input v-model="form.sceneName" placeholder="请输入场景名称" />
        </el-form-item>
        <el-form-item label="开始时间" prop="sceneStartTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.sceneStartTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="sceneEndTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.sceneEndTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="报道截止时间" prop="signEndTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.signEndTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择报道截止时间">
          </el-date-picker>
        </el-form-item>
        <!--<el-form-item label="创建人" prop="createUser">
          <el-input v-model="form.createUser" placeholder="请输入创建人" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 生成二维码对话框 -->
    <el-dialog :title="qrtitle" :visible.sync="qropen" width="500px" append-to-body>
      <div align="center">
        <span style="font-size: 18px">{{grantName}}</span>
        <span style="font-size: 18px">({{sceneName}})</span>
        <div id="qrCode" ref="qrCodeUrl">
        </div>
      </div>
    </el-dialog>
  </div>


</template>

<script>
import { listScene, getScene, delScene, addScene, updateScene, exportScene } from "@/api/grant/scene";
import { listExampoint } from  "@/api/grant/exampoint";
import {listGrantAll} from "@/api/grant/grant";
import QRCode from "qrcodejs2";

export default {
  name: "Scene",
  data() {
    return {
      qropen: false,
      qrFlag: false,
      grantName: undefined,
      sceneName: undefined,
      qrtitle: '签到二维码',
      enrollHost: undefined, //报名的域名，从后台系统配置表获取
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 签到场次表格数据
      sceneList: [],
      grantOptions: [],
      exampointOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        grantId: undefined,
        exampointId: undefined,
        sceneName: undefined,
        sceneStartTime: undefined,
        sceneEndTime: undefined,
        signEndTime: undefined,
        createUser: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    let query ={};
    query["isEnabled"]="0";
    listGrantAll(query).then(res=>{
      this.grantOptions = res.data;
      this.queryParams.grantId=res.data[res.data.length-1].grantId;
      this.getList();
    })

  },
  methods: {
    /** 查询签到场次列表 */
    getList() {
      this.loading = true;
      listScene(this.queryParams).then(response => {
        this.sceneList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    grantSelectChange(value){

      this.queryParams.grantId = value;
      this.getList();
    },
    grantSelectChangeByForm(value){

      let grantTypeId = 0;
      Object.keys(this.grantOptions).map((key)=>{
        if(this.grantOptions[key].grantId==value){
          grantTypeId = this.grantOptions[key].granttypeId;
          return;
        }
      })
      listExampoint({'granttypeId':grantTypeId}).then(response => {
        this.exampointOptions = response.rows;
        if(this.exampointOptions.length>0)
        {
          this.form.exampointId = this.exampointOptions[0].id
        }

      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        grantId: undefined,
        exampointId: undefined,
        sceneName: undefined,
        sceneStartTime: undefined,
        sceneEndTime: undefined,
        signEndTime: undefined,
        createUser: undefined,
        createTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加签到场次";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getScene(id).then(response => {
        this.form = response.data;
        let grantTypeId = 0;
        Object.keys(this.grantOptions).map((key)=>{
          if(this.grantOptions[key].grantId==this.form.grantId){
            grantTypeId = this.grantOptions[key].granttypeId;
            return;
          }
        })
        listExampoint({'granttypeId':grantTypeId}).then(response => {
          this.exampointOptions = response.rows;
          this.open = true;
          this.title = "修改签到场次";

        });

      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateScene(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addScene(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除签到场次编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delScene(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有签到场次数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportScene(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    /** 生成签到二维码 */
    getSceneQrde(row){
      this.qropen = true;
      if(this.qrFlag) {
        this.delQrcode();
      }
      this.qrFlag = true;
      this.qrtitle = '签到二维码';
      // let enrollUrl=this.enrollHost;
      let enrollUrl = row.enrollHost;
      this.grantName = row.grantName;
      this.sceneName = row.sceneName;
      setTimeout(function() {
        enrollUrl+="/#/signIn?grantId="+row.grantId+"&sceneId="+row.id;
        console.log(enrollUrl);
        let a = new QRCode('qrCode', {
          text: enrollUrl, // 需要转换为二维码的内容
          width: 200,
          height: 200,
          colorDark: "black", //#000000为黑色
          colorLight: "white",
        });
      },200);

    },
    getRpSceneQrde(row){
      this.qropen = true;
      if(this.qrFlag) {
        this.delQrcode();
      }
      this.qrFlag = true;
      this.qrtitle = '人员替换二维码';
      // let enrollUrl=this.enrollHost;
      let enrollUrl = row.enrollHost;
      this.grantName = row.grantName;
      this.sceneName = row.sceneName;
      setTimeout(function() {
        enrollUrl+="/#/rpExam?sceneId="+row.id;
        console.log(enrollUrl);
        let a = new QRCode('qrCode', {
          text: enrollUrl, // 需要转换为二维码的内容
          width: 200,
          height: 200,
          colorDark: "black", //#000000为黑色
          colorLight: "white",
        });
      },200);

    }
    // 清空二维码,避免生成多个二维码
    ,delQrcode(){
      this.$refs.qrCodeUrl.innerHTML = ""
    }
  }
};
</script>
